﻿#include "toast.h"

#include <QtWidgets\QDialog>
#include <QtWidgets\QHBoxLayout>
#include <QtWidgets\QLabel>
#include <QEvent>

class ToastDlg : public QDialog
{
private:
	QLabel* mLabel;
	QLabel* mCloseBtn;
protected:
	bool eventFilter(QObject *obj, QEvent *ev) override
	{
		if (obj == mCloseBtn)
		{
			if (ev->type() == QEvent::MouseButtonRelease)
			{
				accept();
			}
		}
		return QObject::eventFilter(obj, ev);
	}
public:
	ToastDlg()
	{
		auto layout = new QHBoxLayout;//水平布局

		mLabel = new QLabel(this);
		//mLabel->setStyleSheet("QLabel{color: white; background:transparent;border-radius:1px;}");//red
		layout->addWidget(mLabel, 1);//stretch = 1 label和button之间留1行空白

		mCloseBtn = new QLabel(this);
		//mCloseBtn->setPixmap(QPixmap(":/res/img/close.png"));
		mCloseBtn->installEventFilter(this);
		/*mCloseBtn->setStyleSheet("color: white; background:transparent;background:transparent;");*/
		mCloseBtn->setStyleSheet("background:transparent");
		layout->addWidget(mCloseBtn);
		//layout->setContentsMargins(1,1,1,1);

		setLayout(layout);

		setWindowFlag(Qt::FramelessWindowHint);//生成一个无边界窗口。用户不能通过窗口系统移动或调整无边界窗口的大小。
		setAttribute(Qt::WA_ShowWithoutActivating, true);//Show the widget without making it active.
		setAttribute(Qt::WA_TranslucentBackground, true);   // 背景透明
	}

	void show(Toast::Level level, const QString& text)
	{
		QPalette p = palette();

		//QColor(int r, int g, int b, int a = 255)
		//Constructs a color with the RGB value r, g, b, and the alpha-channel (transparency) value of a.
		//p.setColor(QPalette::Window, QColor(0, 0, 0, 200));
		//p.setColor(QPalette::Window, QColor(45, 53, 76));
		if (level == Toast::TOAST_INFO) {
			//p.setColor(QPalette::Window, QColor(0, 0, 0, 200));//black
			mLabel->setStyleSheet("QLabel{font: normal normal 20px;background-color:rgba(0, 0, 0, 100);border-radius:10px;}");
		}
		else if (level == Toast::TOAST_WARN) {
			//p.setColor(QPalette::Window, QColor(0, 0, 255, 200));//blue
			mLabel->setStyleSheet("QLabel{font: normal normal 20px;background-color:rgba(0, 0, 255, 100);border-radius:10px;}");
		}
		else {
			//p.setColor(QPalette::Window, QColor(255, 0, 0, 200));//red
			mLabel->setStyleSheet("QLabel{font: normal normal 20px;background-color:rgba(255, 0, 0, 100);border-radius:10px;}");
		}
		setPalette(p);//set widget's palette

		mLabel->setText(text);
		mLabel->setAlignment(Qt::AlignHCenter);
		setWindowFlag(Qt::WindowStaysOnTopHint);//通知窗口系统该窗口应保持在所有其他窗口的顶部
		QDialog::show();
	}
};//~class ToastDlg end


Toast::Toast()//¹¹Ôìº¯Êý
{
	mDlg = new ToastDlg;
}

//返回一个实例(instance)
Toast &Toast::instance()
{
	static Toast thiz;//这种实例化方法会自动回收内存
	return thiz;
}

void Toast::show(Toast::Level level, const QString &text)
{
	mDlg->show(level, text);//ToastDlg.show方法
	if (mTimerId != 0)//int mTimerId
	{
		//如果之前已经开启了一个定时器，先把他关掉
		killTimer(mTimerId);
	}
	mTimerId = startTimer(1000);//启动定时器，每1s触发定时器事件，直到调用killTimer
}

//重写定时器事件回调函数
void Toast::timerEvent(QTimerEvent *event)
{
	killTimer(mTimerId);
	mTimerId = 0;
	mDlg->accept();//隐藏模态对话框
	//mDlg->hide();
}